package com.xframework.security.util;

import com.xframework.Base;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
@ConfigurationProperties(prefix = "security")
public class Constant {
    //session的Key
    public final static String SECURITY_ACCOUNT = "SECURITY_ACCOUNT";
    public final static String UNDERLINE = "_";
    public final static String SECURITY_RANDOM_ = "SECURITY_RANDOM_";
    public final static String SECURITY_EXPIRATION_ = "SECURITY_EXPIRATION_";
    public final static String ACCESS_CONTROL_EXPOSE_HEADERS = "Access-Control-Expose-Headers";
    public final static String Authorization = "Authorization";
    public final static String X_FORWARDED_FOR = "X-Forwarded-For";
    public final static String X_REAL_IP = "X-Real-IP";
    public final static String ACCOUNT_NAME = "accountName";
    public final static String MESSAGE = "message";
    public final static String MENU_INIT_ID = "0";

    public static String securityJwtKey = "xframework";
    public static int securityJwtExpiration = 600 * 1000;
    public static boolean securityJwtDelay = Boolean.TRUE;
    public static String[] securityIncludeUrlPatterns;
    public static String[] securityExcludeUrlPatterns;
    public static String securityApplicationId;
    public static String securityLoginUrl;
    public static String securityLogoutUrl;
    public static String securityExceptionUrl;
    public static String securityAccountErrorMessage = "9999 用户名或密码错误";
    public static String securityExpirationMessage = "9999 登陆超时";
    public static String securityInvalidMessage = "9999 用户无访问权限";

    public Constant() {

    }

    public void setSecurityJwtKey(String securityJwtKey) {
        Constant.securityJwtKey = securityJwtKey;
    }

    public void setSecurityJwtExpiration(int securityJwtExpiration) {
        Constant.securityJwtExpiration = securityJwtExpiration;
    }

    public void setSecurityJwtDelay(boolean securityJwtDelay) {
        this.securityJwtDelay = securityJwtDelay;
    }

    public void setSecurityApplicationId(String securityApplicationId) {
        Constant.securityApplicationId = securityApplicationId;
    }

    public void setSecurityLoginUrl(String securityLoginUrl) {
        Constant.securityLoginUrl = securityLoginUrl;
    }

    public void setSecurityLogoutUrl(String securityLogoutUrl) {
        Constant.securityLogoutUrl = securityLogoutUrl;
    }

    public void setSecurityExceptionUrl(String securityExceptionUrl) {
        Constant.securityExceptionUrl = securityExceptionUrl;
    }

    public void setSecurityAccountErrorMessage(String securityAccountErrorMessage) {
        Constant.securityAccountErrorMessage = securityAccountErrorMessage;
    }

    public void setSecurityExpirationMessage(String securityExpirationMessage) {
        Constant.securityExpirationMessage = securityExpirationMessage;
    }

    public void setSecurityInvalidMessage(String securityInvalidMessage) {
        Constant.securityInvalidMessage = securityInvalidMessage;
    }

    public void setSecurityIncludeUrlPatterns(String[] securityIncludeUrlPatterns) {
        Constant.securityIncludeUrlPatterns = securityIncludeUrlPatterns;
    }

    public void setSecurityExcludeUrlPatterns(String[] securityExcludeUrlPatterns) {
        Constant.securityExcludeUrlPatterns = securityExcludeUrlPatterns;
    }

    /**
     * 通过正则表达式匹配字符串
     *
     * @param regex String
     * @param input CharSequence
     * @return boolean
     */
    public static boolean matches(String regex, CharSequence input) {
        if (regex == null || input == null) {
            return true;
        } else if (regex.contains(input)) {
            return true;
        } else if (regex.indexOf(Base.STAR) > -1) {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(input);
            return m.find();
        } else {
            return false;
        }
    }

}
